﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//    Helpers.cs
//
// Abstract:
//
//    Class for helper methods
//
// Classes:
//
//    Microsoft.EnhancedReminders.OutlookAddIn.UnitTests.Helpers
//
//--

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.ObjectModel;

using Microsoft.OfficeCommunicationsServer.Applications.Common;

namespace Microsoft.EnhancedReminders.UnitTests
{
    /// <summary>
    /// Class to provide useful routines for DRTs
    /// </summary>
    public static class Helpers
    {
        /// <summary>
        /// Calls WaitOne() on the specified EventWaitHandle, waiting 30 seconds and then throwing
        /// an exception. If the debugger is attached, it waits forever.
        /// </summary>
        /// <param name="waitEvent">The EventWaitHandle.</param>
        /// <param name="errorMessage">Failure message passed to assert() if we time out</param>
        public static void WaitOne<T>(T waitEvent, string errorMessage) where T : EventWaitHandle
        {
            Debug.Assert(null != waitEvent, "null != waitEvent");
            Debug.Assert(!String.IsNullOrEmpty(errorMessage), "!String.IsNullOrEmpty(errorMessage)");

            WaitOne(waitEvent, 5000, errorMessage);
        }

        /// <summary>
        /// Calls WaitOne() on the specified EventWaitHandle, the specified time and then throwing
        /// an exception. If the debugger is attached, it waits forever.
        /// </summary>
        /// <param name="waitEvent">The EventWaitHandle.</param>
        /// <param name="timeout">Time to wait</param>
        /// <param name="errorMessage">Failure message passed to assert() if we time out</param>
        public static void WaitOne<T>(T waitEvent, int timeout, string errorMessage) where T : EventWaitHandle
        {
            Debug.Assert(null != waitEvent, "null != waitEvent");
            Debug.Assert(!String.IsNullOrEmpty(errorMessage), "!String.IsNullOrEmpty(errorMessage)");

            int msecToWait = System.Diagnostics.Debugger.IsAttached ? Timeout.Infinite : timeout;
            Assert.IsTrue(waitEvent.WaitOne(msecToWait, false), errorMessage);
        }
        
        /// <summary>
        /// Creates an enabled NotificationItem with the specified ID and time.
        /// </summary>
        /// <param name="id">ID of the NotificationItem</param>
        /// <param name="time">Time of the NotificationItem</param>
        /// <remarks>
        /// The NotificationItem is considered enabled because it has enabled channels.
        /// </remarks>
        /// <returns>
        /// The NotificationItem.
        /// </returns>
        public static CalendarNotificationItem CreateNotificationItem(string id, DateTime time)
        {
            // Create a channel list for the reminder so that it appears enabled
            NotificationChannelListLeadTime channelList = new NotificationChannelListLeadTime(CreateDummyChannels());
            channelList.LeadTime = 15;

            return new CalendarNotificationItem(Context.Global, new NotificationId(id, "john@contoso.com"), "john@contoso.com", time, 
                new ReminderItem(channelList, channelList, null), "John Contoso", false, "conf room 101", "Hello", time);
        }

        /// <summary>
        /// Creates a disbaled NotificationItem with the specified ID and time.
        /// </summary>
        /// <param name="id">ID of the NotificationItem</param>
        /// <param name="time">Time of the NotificationItem</param>
        /// <remarks>
        /// The NotificationItem is considered disbaled because it has no channels.
        /// </remarks>
        /// <returns>
        /// The NotificationItem.
        /// </returns>
        public static CalendarNotificationItem CreateDisabledNotificationItem(string id, DateTime time)
        {
            return new CalendarNotificationItem(Context.Global, new NotificationId(id, "john@contoso.com"), "john@contoso.com", time,
                new ReminderItem(new List<NotificationChannel>()), "John Contoso", false, "conf room 101", "Hello", time);
        }

        /// <summary>
        /// Returns a set of dummy channels intended to simulate the default channels in the Outlook Storage Item
        /// </summary>
        /// <returns></returns>
        public static ObservableCollection<NotificationChannel> CreateDummyChannels()
        {
            ObservableCollection<NotificationChannel> dummyChannels = new ObservableCollection<NotificationChannel>();

            dummyChannels.Add(new NotificationChannel(NotificationChannelType.Communicator, "Communicator", "john@contoso.com"));
            dummyChannels.Add(new NotificationChannel(NotificationChannelType.Phone, "Mobile", "4255551234"));

            return dummyChannels;
        }
    }
}
